/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.ui.widgets;

import javax.microedition.lcdui.Graphics;

public class ShadowText extends Widget {
	private int shadXoff = 1;
	private int shadYoff = 1;
	// private int shadowColor = 0x808080;
	private String text;

	public ShadowText(Theme theme, String text) {
		super(theme, 0, 0);
		this.text = text;
		updateModel();
	}

	public void cleanUp() {
		text = null;
	}

	public void render(Graphics g) {
		if (!isVisible)
			return;
		capture(g, width, height, false);
		int centerX = width / 2
				- (theme.bmFont != null ? theme.bmFont.stringWidth(text) / 2 : theme.widgetFont.stringWidth(text) / 2);
		int centerY = height / 2
				- (theme.bmFont != null ? theme.bmFont.getHeight() / 2 : theme.widgetFont.getHeight() / 2);
		g.setFont(theme.widgetFont);
		g.setColor(theme.bgColor);
		if (theme.bmFont != null) {
			theme.bmFont.drawString(g, text, centerX + shadXoff, centerY + shadYoff, 0);
		} else
			g.drawString(text, centerX + shadXoff, centerY + shadYoff, 0);
		g.setColor(theme.fgColor);
		if (theme.bmFont != null) {
			theme.bmFont.drawString(g, text, centerX, centerY, 0);
		} else
			g.drawString(text, centerX, centerY, 0);
		release(g, width, height, false);

	}

	public void setShadowOffset(int xOffset, int yOffset) {
		this.shadXoff = xOffset;
		this.shadYoff = yOffset;
		updateModel();
		reDraw();
	}

	public void setText(String text) {
		this.text = text;
		updateModel();
		reDraw();
	}

	protected void updateModel() {
		this.width = 4 + Math.abs(shadXoff)
				+ (theme.bmFont != null ? theme.bmFont.stringWidth(text) : theme.widgetFont.stringWidth(text));
		this.height = 4 + Math.abs(shadYoff)
				+ (theme.bmFont != null ? theme.bmFont.getHeight() : theme.widgetFont.getHeight());

	}
}
